// pages/message/message.js
// 获取应用实例
const app = getApp()
const requests = require('../../config/request')
var util = require('./../../utils/util.js');
var api = require('./../../config/api.js');


Page({

    /**
     * 页面的初始数据
     */
    data: {
        loadTimeInterval: true,
        commentList: [],
        commentPage: 1,
        commentContent: '',
        commentPid: 0,
        showInput: false,
        commentEnd: false,
        isClearComment: false,
        isRefresh: false,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.getCommentList(true)
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        this.setData({isRefresh: true})
        this.getCommentList(false)
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },

    getCommentList: function(isShowLoading){
        var that = this;
        var postData = {};
        if (this.data.isRefresh){
          postData.page = 1;
          this.setData({isRefresh: false})
        }else{
          postData.page = this.data.commentPage;
        }
        
        postData.token = app.globalData.userInfo.token;
        console.log('page:' + this.data.commentPage)
        util.myRequest(api.GetCommentList, postData, function(data){
          if (data.data.length < 10){
            that.setData({commentEnd: true})
          }
          var newCommentList = [];
          var oldCommentList = that.data.commentList;
          for (var i=0;i<data.data.length;i++){
            var isRepeat = false
            for (var j=0;j<oldCommentList.length;j++){
              if (data.data[i].cId == oldCommentList[j].cId){
                isRepeat = true;
                oldCommentList[j] = data.data[i]
              }
            }
            if (!isRepeat){
              newCommentList.push(data.data[i])
            }
          }
          var nowList = oldCommentList.concat(newCommentList);
          if (postData.page == 1){
            nowList = newCommentList.concat(oldCommentList)
          }
          try {
            wx.setStorageSync('commentList', nowList)
          } catch (e) { }
          that.setData({commentList: nowList});
        }, false, false, isShowLoading);
      },
    
      getCommentStorge: function(){
        try {
          var commentList = wx.getStorageSync('commentList')
          return commentList
        } catch (e) {
          return []
        }
      },

      toPost(){
        wx.navigateTo({
          url: '../post/post'
        })
      },
    
      bindReplaceInput(e){
        this.setData({commentContent: e.detail.value});
      },
    
      showInput(e){
        this.setData({commentPid:  e.currentTarget.dataset.id})
        this.setData({showInput: true})
      },
    
      inputBlur(e){
        this.setData({showInput: false})
      },
    
      previewImg(e){
        for (var i=0;i<this.data.commentList.length;i++){
          if (this.data.commentList[i].cId == e.currentTarget.dataset.id){
            wx.previewImage({
              current: e.currentTarget.dataset.url, // 当前显示图片的http链接
              urls: this.data.commentList[i].picList // 需要预览的图片http链接列表
            })
          }
        }
    
        wx.previewImage({
          current: '', // 当前显示图片的http链接
          urls: [] // 需要预览的图片http链接列表
        })
      },
    
      sendComment(){
        var that = this;
        var postData = {};
        postData.token = app.globalData.userInfo.token;
        postData.cPid = this.data.commentPid;
        postData.cContent = this.data.commentContent;
        postData.pgId = 0
        util.myRequest(api.AddComment, postData, function(data){
          // that.getCommentList()
          var newCommentList = that.data.commentList;
          for (var i=0;i<newCommentList.length;i++){
            if (newCommentList[i].cId == that.data.commentPid){
                var replyInfo = {}
                replyInfo.cContent = that.data.commentContent
                replyInfo.userInfo = {uName: app.globalData.userInfo.uName}
                newCommentList[i].replyList.push(replyInfo)
                that.setData({commentList: newCommentList, commentContent: ''});
                console.log(newCommentList)
            }
          }
        });
      },
    
      atCommentBottom: function(){
        console.log('dddddddddddddd')
        console.log(this.data.commentEnd)
        console.log(this.data.loadTimeInterval)
        var that = this;
        if (this.data.commentEnd){
          return
        }
        if (this.data.loadTimeInterval){
          that.setData({loadTimeInterval: false});
    
          that.setData({commentPage: that.data.commentPage + 1})
          that.getCommentList(false)
    
    
          setTimeout(function(){
            that.setData({loadTimeInterval: true});
          }, 1500);
        }
      }
})